<template>
    <div>
        <h1>科室录入</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">姓名</td>
                    <td><input type="text" v-model="info.YSName"></td>
                </tr>

                <tr>
                    <td align="right">头像</td>
                    <td><input type="file" @change="imgs">
                        <img :src="info.Img" width="100" height="100" v-if="info.Img != ''">
                    </td>

                </tr>

                <tr>
                    <td align="right">科室</td>
                    <td>
                        <select v-model="info.KSId">
                            <option value="">请选择</option>
                            <option :value="itme.Id" v-for="itme in lisr" :key="itme.Id">{{ itme.Name }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">创建日期</td>
                    <td><input type="date" v-model="info.YsTime"></td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="录入" @click="ok"></td>
                </tr>

            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
let info: any = reactive({
    "YSName": "",
    "Img": "",
    "KSId": "",
    "YsTime": ""
})
let lisr: any = ref([]);
onMounted(() => {
    getlisr();
})
//=================================添加
const ok = () => {
    axios({
        url: '/api/YS/YSAdd',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert("姓名重复");
                return;
            }
            if (res.data > 0) {
                alert("录入成功");
                router.push('/ysshow');
                return;
            }
            else {
                alert("录入失败");
                return;
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
//=====================================上传文件
const imgs = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: '/api/YS/Imgs',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == "大于2M") {
                alert("您上传的图片大于2M")
            }
            info.Img = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
//================================================
const getlisr = () => {
    axios({
        url: '/api/KS/KSShow',
        method: 'get',

    })
        .then((res) => {
            console.log(res);
            lisr.value = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>